const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  const loginname = '槽边往事';
  let avatar_url = 'https://taosha01-1253585015.cos.ap-shanghai.myqcloud.com/typora/caobianwangshi.jpeg';

  let user = await db.collection('users').findOne({loginname});

  await db.collection('users').updateOne({_id: user._id}, {
    $set: {avatar_url},
  });


  let fans = await db.collection('users').find({'followings.name': loginname}).toArray();
  for (const fan of fans) {
    let followings = fan.followings;
    for (const following of followings) {
      if (following.name == loginname) {
        following.logo = avatar_url;
        await db.collection('users').updateOne({_id: fan._id}, {
          $set: {followings},
        });
      }
    }
  }

  await db.collection('topics').updateMany({author_name: loginname}, {
    $set: {author_avatar: avatar_url},
  });


  client.close();

}

main();
